package net.qhzw.vip.qiyu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.dto.DeliveryResultDto;
import net.qhzw.vip.qiyu.dto.ShipTraceDto;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.OrderService;
import net.qhzw.vip.qiyu.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("test")
@Api(tags = "后台管理系统的测试接口")
@Slf4j
public class TestController {

    @Value("${alicloud.app-code}")
    private String appCode;

    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMsg/{message}")
    public String sendMsg(@PathVariable String message) {
        log.info("当前时间：{},发送一条信息给两个 TTL 队列:{}", new Date(), message);
        rabbitTemplate.convertAndSend("X", "XA", "消息来自TTL为10s队列QA：" + message);

        return "发送成功";
    }


    @GetMapping("hello")
    public String hello() {


        /*

         */
        return "hello";
    }




    @GetMapping("queryShop")
    public JsonResult queryShop(String shipCode, String shipNo) {
        ShipTraceDto shipTrace = new ShipTraceDto();
        try {
            String orderTracesByJson = getOrderTracesByJson(shipCode, shipNo);
            if (!StringUtils.isEmpty(orderTracesByJson)) {
                shipTrace = JSONObject.parseObject(orderTracesByJson, ShipTraceDto.class);
            }
            if ("ok".equals(shipTrace.getMsg())) {
                DeliveryResultDto result = shipTrace.getResult();
                return JsonResult.ok(result);
            }

        } catch (Exception e) {
            log.error("[快递鸟 查询] 异常", e);
        }
        return null;
    }

    /**
     * Json方式 查询订单物流轨迹
     *
     * @throws Exception
     */
    private String getOrderTracesByJson(String shipCode, String shipNo) {
        log.info("快递公司识别编码 : {}, 订单号为: {}", shipCode, shipNo);
        //https://wuliu.market.alicloudapi.com/kdi
        String host = "https://wuliu.market.alicloudapi.com";
        String path = "/kdi";
        String method = "GET";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<>();
        querys.put("no", shipNo.trim());
        querys.put("type", shipCode);
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取response的body
            String ss = EntityUtils.toString(response.getEntity());
            log.info("查询到的数据为: {}", ss);
            JSONObject jsonObject = JSONObject.parseObject(ss);
            return JSON.toJSONString(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }




}
